package com.huangd.platform.common.utils;

import java.util.Collection;

/**
 * 工具类
 *
 * @author wing
 */
public class Utils {

	public static boolean isEmpty(String content) {

		return content == null || content.trim().isEmpty();
	}

	public static boolean notEmpty(String content) {

		return isEmpty(content) == false;
	}

	public static String emptyIsNull(String content) {

		if (content == null || content.isEmpty())
			return null;

		content = content.trim();
		return content.isEmpty() ? null : content;
	}

	public static int emptyIsZero(String content) {

		if (content == null || content.isEmpty())
			return 0;

		content = content.trim();
		return content.isEmpty() ? 0 : Integer.parseInt(content);
	}

	public static String nullIsEmpty(String content) {
		return content == null ? "" : content;
	}

	public static Integer zeroIsNull(Integer value) {

		return value == 0 ? null : value;
	}

	public static int nullIsZero(Integer value) {

		return value == null ? 0 : value;
	}

	public static String nullIsZero(String value) {
		return value == null ? "0" : value;
	}

	public static long nullIsZero(Long value) {

		return value == null ? 0 : value;
	}

	public static <E> int contains(Collection<E> source, Collection<E> target) {

		int count = 0;

		for (E obj : target) {
			if (source.contains(obj)) {
				++count;
			}
		}
		return count;
	}

	public static String filterUtf8mb4(String str) {
		final int LAST_BMP = 0xFFFF;
		StringBuilder sb = new StringBuilder(str.length());
		for (int i = 0; i < str.length(); i++) {
			int codePoint = str.codePointAt(i);
			if (codePoint < LAST_BMP) {
				sb.appendCodePoint(codePoint);
			} else {
				i++;
			}
		}
		return sb.toString();
	}
}
